package com.fmob.client.app.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;

import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.FileUtil;
import sdk.webview.fmc.com.fmcsdk.util.SdkVersionUtils;

/**
 * 版本更新自动安装后删除APK
 * Created by ydm on 2018/3/6.
 */

public class InitApkBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
//            XLog.i("监听到系统广播添加");
        }

        if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
            String applicationId = context.getApplicationInfo().packageName;
            String dataString = intent.getDataString();
            Log.i("Media","监听到系统广播删除");
//            XLog.i("监听到系统广播移除"+"移除的包名："+dataString.substring(8,dataString.length())+"本包名："+applicationId);
        }

        if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
            String applicationId = context.getApplicationInfo().packageName;
            String dataString = intent.getDataString();
            Log.i("Media","监听到系统广播替换"+"替换的包名："+dataString.substring(8,dataString.length())+"本包名："+applicationId);
            //如果是本项目更新，删除原文件下的APK
            if ( dataString.substring(8,dataString.length()).equals(applicationId)){

//                String storePath;
//                if (SdkVersionUtils.checkedAndroid_R()) {
//                    storePath =context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk");
//                } else {
//                    storePath = context.getFilesDir().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk");
//
//                }
//                String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/fmc/" + PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.APP_NAME, "fmsdk");
//                FileUtil.deleteApkFile(storePath+Constant.APKPATH);
//                Uri uri=Uri.parse("package:com.fmob.client.app.fms");
//                Intent intent2 = new Intent(Intent.ACTION_DELETE,uri);
//                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(intent2);
            }
        }
    }
}
